
const crypto = require('crypto-js')
const { encode } = require('js-base64/base64.js')


// 计算签名。
function computeSignature(accessKeySecret, canonicalString) {
  return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
}

const date = new Date();
date.setHours(date.getHours() + 1);
const policyText = {
  expiration: date.toISOString(), // 设置policy过期时间。
  conditions: [
    // 限制上传大小。
    ["content-length-range", 0, 1024 * 1024 * 1024],
  ],
};

async function getFormDataParams(AccessKeyId, AccessKeySecret, SecurityToken) {
  const policy = encode(JSON.stringify(policyText)) // policy必须为base64的string。
  const signature = computeSignature(AccessKeySecret, policy)
  const formData = {
    OSSAccessKeyId: AccessKeyId,
    signature,
    policy,
    'x-oss-security-token': SecurityToken
  }
  return formData
}



module.exports = getFormDataParams